package com.example.colorpickdemo.ui.activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.colorpickdemo.R;

import com.example.colorpickdemo.util.ToastUtil;
import com.mt.sdk.help.BleListenCallBack;
import com.mt.sdk.help.CameraListenCallBack;
import com.mt.sdk.help.MTColorPickHelper;
import com.mt.sdk.help.bean.ReciveMsg;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;

import androidx.appcompat.app.AppCompatActivity;

import zhou.colorpalette.ColorSelectDialog;


//蓝牙处理类
public class ColorPickTestActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText sendCommonMsg;
    private Button sendCommonBtn;
    private Button sendCommonBtn2;

    private EditText setRGBCodeMsg;
    private Button pickRGBCode;
    private Button setRGBCode;


    private EditText setBulletsMsg;
    private EditText setRecoilModeMsg;
    private Button setShotMode;

    private Button setInternalRecoilMode;
    private Button setExternalRecoilMode;

    private Button sendRequestQuantity;
    private Button sendRequestBullets;
    private Button clearReadDataLog;

    private Button sendHeartbeatCommands;


    private ScrollView mScrollView;
    private TextView mDumpTextView;


    private MTColorPickHelper mMTColorPickHelper;

    private BleListenCallBack mBleListenCallBack = new  BleListenCallBack(){

        @Override
        public void onDatasRecive(ReciveMsg reciveMsg) {
            Message msg = new Message();
            msg.what=2;
            msg.obj = reciveMsg.getReciveDataString();
            mHandler.handleMessage(msg);

            msg.what=3;
            StringBuffer sb = new StringBuffer();
            sb.append("commandId:"+reciveMsg.getCommandId());
            sb.append(";directionCode:"+reciveMsg.getDirectionCode());
            sb.append(";length:"+reciveMsg.getLength());

            sb.append(";keyStatus:"+reciveMsg.getKeyStatus()); 
            sb.append(";loading:"+reciveMsg.getLoading());
            sb.append(";gunBolt:"+reciveMsg.getGunBolt());
            sb.append(";trigger:"+reciveMsg.getTrigger());

            sb.append(";batteryProtection:"+reciveMsg.getBatteryProtection());
            sb.append(";dropGun:"+reciveMsg.getDropGun());
            sb.append(";lackOfWater:"+reciveMsg.getLackOfWater());
            sb.append(";recoilFailure:"+reciveMsg.getRecoilFailure());
            sb.append(";lowPower:"+reciveMsg.getLowPower());
            sb.append(";powerOnState:"+reciveMsg.getPowerOnState());

            sb.append(";gameType:"+reciveMsg.getGameType());

            sb.append(";positionFeedback:"+reciveMsg.getPositionFeedback());
            sb.append(";macAddress:"+reciveMsg.getMacAddress());

            sb.append(";commandFeedback:"+reciveMsg.getCommandFeedback());
            sb.append(";statusFeedback:"+reciveMsg.getStatusFeedback());
            sb.append(";dataFeedback:"+reciveMsg.getDataFeedback());

            sb.append(";bulletsNum:"+reciveMsg.getBulletsNum());
            sb.append(";quantityNum:"+reciveMsg.getQuantityNum());
            sb.append(";faultCode:"+reciveMsg.getFaultCode());



            msg.obj = sb.toString();
            mHandler.handleMessage(msg);

//            HttpHelper tHttpHelper = new HttpHelper();
//                tHttpHelper.sendCalibrationJsonPost(new HttpHelper.HttpListener(){
//
//                    @Override
//                    public void onFail(String returnData) {
//                        Message msg = Message.obtain();
//                        msg.what = 5;
//                        msg.obj = returnData;
//                        mHandler.sendMessage(msg);
//                    }
//
//                    @Override
//                    public void onSuccess(String returnData) {
//                        Message msg = Message.obtain();
//                        msg.what = 6;
//                        msg.obj = returnData;
//                        mHandler.sendMessage(msg);
//                    }
//                });
        }
    };
    private CameraListenCallBack mCameraListenCallBack = new CameraListenCallBack(){
        @Override
        public void onDatasRecive(String s) {
            Message tMessage = new Message();
            tMessage.what=4;
            tMessage.obj = s;
            mHandler.handleMessage(tMessage);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_furance_test);
        mMTColorPickHelper = MTColorPickHelper.getinstance();
        initView();
        initData();
    }

    private void initView() {
        sendCommonMsg = this.findViewById(R.id.sendCommonMsg);
        sendCommonBtn = this.findViewById(R.id.sendCommonBtn);
        sendCommonBtn2 = this.findViewById(R.id.sendCommonBtn2);

        setRGBCodeMsg = this.findViewById(R.id.setRGBCodeMsg);
        pickRGBCode = this.findViewById(R.id.pickRGBCode);
        setRGBCode = this.findViewById(R.id.setRGBCode);
        setBulletsMsg = this.findViewById(R.id.setBulletsMsg);
        setRecoilModeMsg = this.findViewById(R.id.setRecoilModeMsg);
        setShotMode = this.findViewById(R.id.setShotMode);
        setInternalRecoilMode = this.findViewById(R.id.setInternalRecoilMode);
        setExternalRecoilMode = this.findViewById(R.id.setExternalRecoilMode);

        sendRequestQuantity = this.findViewById(R.id.sendRequestQuantity);
        sendRequestBullets = this.findViewById(R.id.sendRequestBullets);
        clearReadDataLog = this.findViewById(R.id.clearReadDataLog);
        mScrollView = this.findViewById(R.id.demoScroller);
        mDumpTextView = this.findViewById(R.id.consoleText);

        sendHeartbeatCommands = this.findViewById(R.id.sendHeartbeatCommands);
    }

    private void initData() {
        mMTColorPickHelper = MTColorPickHelper.getinstance();
        sendCommonBtn.setOnClickListener(this);
        sendCommonBtn2.setOnClickListener(this);
        pickRGBCode.setOnClickListener(this);
        setRGBCode.setOnClickListener(this);
        setShotMode.setOnClickListener(this);
        setInternalRecoilMode.setOnClickListener(this);
        setExternalRecoilMode.setOnClickListener(this);
        sendRequestQuantity.setOnClickListener(this);
        sendRequestBullets.setOnClickListener(this);
        clearReadDataLog.setOnClickListener(this);
        mMTColorPickHelper.setmBleListenCallBack(mBleListenCallBack);
        mMTColorPickHelper.setmCameraListenCallBack(mCameraListenCallBack);
        sendHeartbeatCommands.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sendCommonBtn:
                if(mMTColorPickHelper.getmBle(1)!=null&&mMTColorPickHelper.getmBle(1).isConnected()){
                    String msg = sendCommonMsg.getText().toString();
                    mMTColorPickHelper.sendCommonMsg(msg,1);
                }else{
                    ToastUtil.showtoast("蓝牙未连接，请先连接");
                }
//                String msg = sendCommonMsg.getText().toString();
//                mMTColorPickHelper.sendCommonMsg(msg,1);
                break;
            case R.id.pickRGBCode:
                ColorSelectDialog colorSelectDialog = new ColorSelectDialog(this);
                colorSelectDialog.setOnColorSelectListener(new ColorSelectDialog.OnColorSelectListener() {
                    @Override
                    public void onSelectFinish(int color) {
                        String colorStr = String.format("%08X", color).substring(2);
                        setRGBCodeMsg.setText(colorStr);
                    }
                });
                int code = 0xffffffff;
                try {
                    String c = setRGBCodeMsg.getText().toString();
                    if (c.length() <= 8) {
                        c = "FF000000".substring(0, 8 - c.length()) + c;
                    }
                    code = new BigInteger(c, 16).intValue();
                } catch (Exception ignored) { }
                colorSelectDialog.setLastColor(code);
                colorSelectDialog.show();
                break;
            case R.id.setRGBCode:
                if(mMTColorPickHelper.getmBle(1)!=null&&mMTColorPickHelper.getmBle(1).isConnected()){
                    String tRGBCodeMsg = setRGBCodeMsg.getText().toString();
                    mMTColorPickHelper.setRGBcode(tRGBCodeMsg,1);
                }else{
                    ToastUtil.showtoast("蓝牙未连接，请先连接");
                }
                break;
            case R.id.setShotMode:
                if(mMTColorPickHelper.getmBle(1)!=null&&mMTColorPickHelper.getmBle(1).isConnected()){
                    String bullets = setBulletsMsg.getText().toString();
                    String recoilMode = setRecoilModeMsg.getText().toString();
                    mMTColorPickHelper.setShotMode(bullets,recoilMode,1);
                }else{
                    ToastUtil.showtoast("蓝牙未连接，请先连接");
                }
//                HttpHelper tHttpHelper = new HttpHelper();
//                tHttpHelper.sendCalibrationJsonPost(new HttpHelper.HttpListener(){
//
//                    @Override
//                    public void onFail(String returnData) {
//                        Message msg = Message.obtain();
//                        msg.what = 5;
//                        msg.obj = returnData;
//                        mHandler.sendMessage(msg);
//                    }
//
//                    @Override
//                    public void onSuccess(String returnData) {
//                        Message msg = Message.obtain();
//                        msg.what = 6;
//                        msg.obj = returnData;
//                        mHandler.sendMessage(msg);
//                    }
//                });
                break;
            case R.id.setInternalRecoilMode:
                if(mMTColorPickHelper.getmBle(1)!=null&&mMTColorPickHelper.getmBle(1).isConnected()){
                    mMTColorPickHelper.setInternalRecoilMode(1);
                }else{
                    ToastUtil.showtoast("蓝牙未连接，请先连接");
                }
                break;
            case R.id.setExternalRecoilMode:
                if(mMTColorPickHelper.getmBle(1)!=null&&mMTColorPickHelper.getmBle(1).isConnected()){
                    mMTColorPickHelper.setExternalRecoilMode(1);
                }else{
                    ToastUtil.showtoast("蓝牙未连接，请先连接");
                }
                break;
            case R.id.sendRequestQuantity:
                if(mMTColorPickHelper.getmBle(1)!=null&&mMTColorPickHelper.getmBle(1).isConnected()){
                    mMTColorPickHelper.sendRequestQuantity(1);
                }else{
                    ToastUtil.showtoast("蓝牙未连接，请先连接");
                }
                break;
            case R.id.sendRequestBullets:
                if(mMTColorPickHelper.getmBle(1)!=null&&mMTColorPickHelper.getmBle(1).isConnected()){
                    mMTColorPickHelper.sendRequestBullets(1);
                }else{
                    ToastUtil.showtoast("蓝牙未连接，请先连接");
                }
                break;
            case R.id.sendCommonBtn2:
                if(mMTColorPickHelper.getmBle(2)!=null&&mMTColorPickHelper.getmBle(2).isConnected()){
                    String msg2 = sendCommonMsg.getText().toString();
                    mMTColorPickHelper.sendCommonMsg(msg2,2);
                }else{
                    ToastUtil.showtoast("蓝牙未连接，请先连接");
                }
//                String msg2 = sendCommonMsg.getText().toString();
//                mMTColorPickHelper.sendCommonMsg(msg2,2);
                break;
            case R.id.clearReadDataLog:
                mDumpTextView.setText("");
                mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
                break;
            case R.id.sendHeartbeatCommands:
                if(mMTColorPickHelper.getmBle(1)!=null&&mMTColorPickHelper.getmBle(1).isConnected()){
                    mMTColorPickHelper.sendHeartbeatCommand(1);
                }else{
                    ToastUtil.showtoast("蓝牙未连接，请先连接");
                }
                break;
        }

    }
    //在主线程里面处理消息并更新UI界面
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    String readMsg = "Send Msg:"+(String)msg.obj+ "\n\n";
                    mDumpTextView.append(readMsg);
                    mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
                    break;
                case 2:
                    String sendMsg = "Read Msg:"+(String)msg.obj+ "\n\n";
                    mDumpTextView.append(sendMsg);
                    mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
                    break;
                case 3:
                    String reciveMsg = "Read reciveMsg:"+(String)msg.obj+ "\n\n";
                    mDumpTextView.append(reciveMsg);
                    mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
                    break;
                case 4:
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS-->");//设置日期格式
                    String cameraData = df.format(new Date())+"Read camera Msg:"+(String)msg.obj+ "\n\n";
                    mDumpTextView.append(cameraData);
                    mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
                    break;
                case 5:
                    String postSData = "Read post Msg:"+(String)msg.obj+ "\n\n";
                    mDumpTextView.append(postSData);
                    mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
                    break;
                case 6:
                    String postFData = "Read post Msg:"+(String)msg.obj+ "\n\n";
                    mDumpTextView.append(postFData);
                    mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
                    break;
                default:
                    break;
            }
            if(mDumpTextView.getLineCount()>200){
                mDumpTextView.setText("");
                mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
            }
        }
    };


}